package org.shj.springcloud.movie;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.ZoneAvoidanceRule;

/**
 * Ribbon 用于客户端侧的负载均衡
 * @author huangjian
 *
 */
@SpringBootApplication
@EnableDiscoveryClient
public class MovieApplication {

	public static void main(String[] args) {
        SpringApplication.run(MovieApplication.class, args);
    }
	
	@Bean
	@LoadBalanced //加上此注解，可以使得客户端按照 Ribbon 的负载均衡策略访问服务端
	public RestTemplate restTemplate(){
		return new RestTemplate();
	}
	
	/*//自定义Ribbon 的负载均衡配置
	@Bean
	@ConditionalOnMissingBean
	public IRule ribbonRule(IClientConfig config){
		//ZoneAvoidanceRule 是一种根据服务提供者所在Zone的性能以及服务提供者可用性综合计算，选择提供者节点的负载均衡规则
		ZoneAvoidanceRule rule = new ZoneAvoidanceRule();
		rule.initWithNiwsConfig(config);
		return rule;
	}*/
}
